<template>
  <el-dialog 
    :model-value="modelValue"
    title="租户统计信息"
    width="500px"
    @update:model-value="$emit('update:modelValue', $event)"
    @open="loadStats"
  >
    <div v-loading="loading">
      <div v-if="stats" class="stats-container">
        <!-- 基础信息 -->
        <div class="stats-section">
          <h4>基础信息</h4>
          <div class="stats-grid">
            <div class="stat-item">
              <div class="stat-label">租户ID</div>
              <div class="stat-value">{{ stats.tenantId }}</div>
            </div>
          </div>
        </div>

        <!-- 用户统计 -->
        <div class="stats-section">
          <h4>用户统计</h4>
          <div class="stats-grid">
            <div class="stat-item">
              <div class="stat-label">
                <el-icon><User /></el-icon>
                总用户数
              </div>
              <div class="stat-value stat-number">{{ stats.userCount || 0 }}</div>
            </div>
            <div class="stat-item">
              <div class="stat-label">
                <el-icon><UserFilled /></el-icon>
                活跃用户
              </div>
              <div class="stat-value stat-number">{{ stats.activeUserCount || 0 }}</div>
            </div>
          </div>
        </div>

        <!-- 配额信息 -->
        <div class="stats-section">
          <h4>配额信息</h4>
          <div class="quota-info">
            <div class="quota-item">
              <div class="quota-label">用户配额状态</div>
              <div class="quota-status">
                <el-tag 
                  :type="stats.canAddUsers ? 'success' : 'danger'"
                  size="large"
                >
                  {{ stats.canAddUsers ? '可添加用户' : '配额已满' }}
                </el-tag>
              </div>
            </div>
            
            <div v-if="stats.remainingQuota >= 0" class="quota-item">
              <div class="quota-label">剩余配额</div>
              <div class="quota-value">
                <span class="remaining-count">{{ stats.remainingQuota }}</span>
                <span class="quota-unit">个用户</span>
              </div>
            </div>
            
            <div v-else class="quota-item">
              <div class="quota-label">配额限制</div>
              <div class="quota-value unlimited">
                <el-icon><Unlock /></el-icon>
                无限制
              </div>
            </div>
          </div>
        </div>

        <!-- 角色统计 -->
        <div class="stats-section">
          <h4>角色统计</h4>
          <div class="stats-grid">
            <div class="stat-item">
              <div class="stat-label">
                <el-icon><Avatar /></el-icon>
                角色数量
              </div>
              <div class="stat-value stat-number">{{ stats.roleCount || 0 }}</div>
            </div>
          </div>
        </div>

        <!-- 活动信息 -->
        <div v-if="stats.lastActiveTime" class="stats-section">
          <h4>活动信息</h4>
          <div class="stats-grid">
            <div class="stat-item">
              <div class="stat-label">
                <el-icon><Clock /></el-icon>
                最后活跃时间
              </div>
              <div class="stat-value">{{ formatDateTime(stats.lastActiveTime) }}</div>
            </div>
          </div>
        </div>
      </div>

      <div v-else-if="!loading" class="no-data">
        <el-empty description="暂无统计数据" />
      </div>
    </div>

    <template #footer>
      <el-button @click="$emit('update:modelValue', false)">关闭</el-button>
      <el-button type="primary" @click="loadStats" :loading="loading">
        <el-icon><Refresh /></el-icon>
        刷新
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { 
  User, 
  UserFilled, 
  Avatar, 
  Clock, 
  Unlock,
  Refresh
} from '@element-plus/icons-vue'
import { tenantApi } from '@/api/tenant'
import type { TenantStatsDTO } from '@/types/api'
import { formatDate } from '@/utils/date'

// Props & Emits
interface Props {
  modelValue: boolean
  tenantId: string
}

const props = defineProps<Props>()

const emit = defineEmits<{
  'update:modelValue': [value: boolean]
}>()

// 响应式数据
const loading = ref(false)
const stats = ref<TenantStatsDTO | null>(null)

// 监听租户ID变化
watch(
  () => props.tenantId,
  () => {
    stats.value = null
  }
)

// 方法
const loadStats = async () => {
  if (!props.tenantId) return

  loading.value = true
  try {
    const response = await tenantApi.getTenantStats(props.tenantId)
    if (response.success) {
      stats.value = response.data
    } else {
      ElMessage.error(response.message || '获取租户统计失败')
      stats.value = null
    }
  } catch (error) {
    console.error('获取租户统计失败:', error)
    ElMessage.error('获取租户统计失败')
    stats.value = null
  } finally {
    loading.value = false
  }
}

const formatDateTime = (dateTime: string) => {
  return formatDate(dateTime, 'YYYY-MM-DD HH:mm:ss')
}
</script>

<style scoped>
.stats-container {
  padding: 8px 0;
}

.stats-section {
  margin-bottom: 24px;
}

.stats-section:last-child {
  margin-bottom: 0;
}

.stats-section h4 {
  margin: 0 0 12px 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  border-bottom: 1px solid var(--el-border-color-light);
  padding-bottom: 8px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  padding: 16px;
  background: var(--el-bg-color-page);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 6px;
  transition: all 0.3s;
}

.stat-item:hover {
  border-color: var(--el-color-primary-light-7);
  background: var(--el-color-primary-light-9);
}

.stat-label {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin-bottom: 8px;
  gap: 6px;
}

.stat-value {
  font-size: 16px;
  font-weight: 500;
  color: var(--el-text-color-primary);
}

.stat-number {
  font-size: 24px;
  font-weight: 600;
  color: var(--el-color-primary);
}

.quota-info {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.quota-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background: var(--el-bg-color-page);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 6px;
}

.quota-label {
  font-size: 14px;
  color: var(--el-text-color-secondary);
}

.quota-status .el-tag {
  font-weight: 500;
}

.quota-value {
  display: flex;
  align-items: center;
  gap: 4px;
}

.remaining-count {
  font-size: 18px;
  font-weight: 600;
  color: var(--el-color-success);
}

.quota-unit {
  font-size: 12px;
  color: var(--el-text-color-secondary);
}

.quota-value.unlimited {
  color: var(--el-color-success);
  font-weight: 500;
  gap: 6px;
}

.no-data {
  padding: 40px 0;
  text-align: center;
}

:deep(.el-empty) {
  padding: 0;
}

:deep(.el-empty__description) {
  color: var(--el-text-color-secondary);
}
</style>

